<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

class Person extends Controller
{
    public function index()
    {
        $user_info = Session::get('user_info');
//dump($user_info);
        $user = Db::name('vip')
            ->where('uid', $user_info['uid'])
            ->find();
//dump($user);
        $class = Db::name('baoming')
            ->where('uid', $user_info['uid'])
            ->select();
//        dump($class);die;
        $student = Db::name('baoming')
            ->where('refertel', $user['utel'])
            ->select();
//        dump($student);
        $utel = [];
        $ucourse = [];
        $reward = [];
        if (!empty($student)) {
//            echo 111;
            foreach ($student as $v) {
                $tel = Db::name('vip')
                    ->field('utel')
                    ->where('uid', $v['uid'])
                    ->find();
                $uc = Db::name('course')
                    ->field('coursename')
                    ->where('id', $v['courseid'])
                    ->find();
                $ac = Db::name('activity')
                    ->field('reward')
                    ->where('id', $v['actid'])
                    ->find();
                $utel[] = $tel['utel'];
                $ucourse[] = $uc['coursename'];
                $reward[] = $ac['reward'];
            }
            $us = [];
            for ($i = 0; $i < count($utel); $i++) {
                $us[] = $utel[$i] . ',' . $ucourse[$i] . ',' . $reward[$i];
            }
//            dump($us);
            $uus = [];
            foreach ($us as $v) {
                $uus[] = explode(',', $v);
            }
        } else {
            $uus = '';
        }
//        dump($uus);
//        die;
        $classid = [];
        foreach ($class as $v) {
            $classid[] = $v['classid'];
        }
        $classename = Db::name('classdate')
            ->where('id', 'in', $classid)
            ->select();
        $cname = [];
        foreach ($classename as $v) {
            $v['starttime'] = date('Y-m-d', $v['starttime']);
            array_push($cname, $v);
        }
        $sql = 'select * from oa_baoming left join oa_activity on oa_baoming.actid = oa_activity.id where oa_baoming.uid =' . $user_info['uid'] . ' group by oa_baoming.actid';
        $act = Db::query($sql);
//        dump($act);die;
        return view('', ['user' => $user, 'classname' => $cname, 'student' => $uus, 'act' => $act]);
    }

    public function info()
    {
        $user = Session::get('user_info');
        $data = [
            'utel' => input('utel'),
            'username' => input('username'),
            'uname' => input('uname'),
            'uemail' => input('uemail'),
            'qq' => input('qq'),
            'uage' => input('uage')
        ];
        $res = Db::name('vip')
            ->where('uid', $user['uid'])
            ->update($data);
        if ($res == 0) {
            $this->success('未进行任何修改', url('index/Person/index'));
        } elseif ($res > 0) {
            $this->success('编辑成功', url('index/Person/index'));
        }
    }

    public function msg()
    {
        $rand = mt_rand(1000, 9999);
        $hdm = '13838063349';
        $test = sendTemplateSMS("13636562641", array($rand, '1'), "1");
        if ($test === true) {
            Session::set('rand', $rand);
            return $rand;
        }
    }

    public function dover()
    {
        $uver = input('uver');
        $rand = Session::get('rand');
        if ($uver == $rand) {
            return view('person/pass');
        } else {
            $this->error('验证码错误');
        }
    }

    public function pass()
    {
        $userid = Session::get('user_info')['uid'];
        $oldpass = Session::get('user_info')['upass'];
        $pass = input('pass');
        if (md5($pass) == $oldpass) {
            $this->error('新密码与原密码相同', url('index/Person/index'));
        }
        if (input('pass') == input('repass')) {
            $res = Db::name('vip')
                ->where('uid', $userid)
                ->update(['upass' => md5($pass)]);
            if ($res >= 0) {
                $this->success('密码修改成功', url('index/Person/index'));
            } else {
                $this->success('密码修改失败', url('index/Person/index'));
            }
        }
    }

    public function userinfo()
    {
        $user_info = Session::get('user_info');
        if (empty($user_info)) {
            return false;
        } else {
            return true;
        }
    }

    public function exit()
    {
        Session::set('user_info', null);
        $this->success('退出成功', 'index/index/index');
    }

//    public function activity()
//    {
//
//    }
}
